// CardConfigDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testadcard.h"
#include "CardConfigDlg.h"
#include ".\\Include\\Driver.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCardConfigDlg dialog


CCardConfigDlg::CCardConfigDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCardConfigDlg::IDD, pParent)
	, m_pCard(NULL)
{
	//{{AFX_DATA_INIT(CCardConfigDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CCardConfigDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCardConfigDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCardConfigDlg, CDialog)
	//{{AFX_MSG_MAP(CCardConfigDlg)
	ON_EN_KILLFOCUS(IDC_ESTART, OnKillfocusEstart)
	ON_BN_CLICKED(IDC_FifoEnable, OnFifoEnable)
	ON_BN_CLICKED(IDC_OVERALLGAIN, OnOverallgain)
	ON_BN_CLICKED(IDC_GAINARRAY, OnGainarray)
	ON_CBN_KILLFOCUS(IDC_INPRANGE, OnKillfocusInprange)
	ON_EN_KILLFOCUS(IDC_PACER, OnKillfocusPacer)
	ON_EN_KILLFOCUS(IDC_CONVERSION, OnKillfocusConversion)
	ON_BN_CLICKED(IDC_CYCLIC, OnCyclic)
	ON_BN_CLICKED(IDC_NONCYCLIC, OnNoncyclic)
	ON_BN_CLICKED(IDC_INTTRIG, OnInttrig)
	ON_BN_CLICKED(IDC_EXTTRIG, OnExttrig)
	ON_BN_CLICKED(IDC_BUFSINGLE, OnBufsingle)
	ON_BN_CLICKED(IDC_BUFDOUBLE, OnBufdouble)
	ON_BN_CLICKED(IDC_DATAUSHORT, OnDataushort)
	ON_BN_CLICKED(IDC_DATAFLOAT, OnDatafloat)
	ON_BN_CLICKED(IDC_EVTENABLE, OnEvtenable)
	ON_BN_CLICKED(IDC_EVTDISABLE, OnEvtdisable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCardConfigDlg message handlers

int CCardConfigDlg::DoModal() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::DoModal();
}

void CCardConfigDlg::SelectCard(CADCard* pcard)
{
	ASSERT(pcard->IsOpened());
	m_pCard = pcard;
}

BOOL CCardConfigDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DEVFEATURES feature;
	char szBuffer[100];
	CADCardControl::Instance()->GetCardFeature(m_pCard, &feature);

	if (feature.usNumGain != 0)
	{
		GetDlgItem(IDC_INPRANGE)->EnableWindow(TRUE);
		for (int i = 0; i < (int)feature.usNumGain; i ++)
			SendDlgItemMessage(IDC_INPRANGE, CB_ADDSTRING, 0,
			(LPARAM)((LPSTR)(feature.glGainList[i].szGainStr)));
		
		SendDlgItemMessage(IDC_INPRANGE, CB_SELECTSTRING, (WPARAM)(-1), 
			(LPARAM)((LPSTR)(feature.glGainList[CADCardControl::Instance()->GetCardGainCode(m_pCard)].szGainStr)));
		
	}
	else
	{
		GetDlgItem(IDC_INPRANGE)->EnableWindow(FALSE);
		SendDlgItemMessage(IDC_INPRANGE, CB_RESETCONTENT,
			0, (LPARAM)((LPSTR)0));
    }

	// Initialize Conversion Number
	ultoa(CADCardControl::Instance()->GetCardConvNum(m_pCard), szBuffer, 10);
	
	SendDlgItemMessage(IDC_CONVERSION, EM_REPLACESEL, 0, (LPARAM)((LPSTR)szBuffer));
	
	// Initialize Pacer Rate
	ultoa(CADCardControl::Instance()->GetCardPacerRate(m_pCard), szBuffer, 10);
	
	SendDlgItemMessage(IDC_PACER, EM_REPLACESEL, 0, (LPARAM)((LPSTR)szBuffer));

	if (CADCardControl::Instance()->GetCardExtTrig(m_pCard))
		GetDlgItem(IDC_PACER)->EnableWindow(FALSE);
	else
		GetDlgItem(IDC_PACER)->EnableWindow(TRUE);
	
	// Initialize Start Channel
	itoa((int)CADCardControl::Instance()->GetCardStartChl(m_pCard), szBuffer, 10);
	
	SendDlgItemMessage(IDC_ESTART, EM_REPLACESEL, 0,
		(LPARAM)((LPSTR)szBuffer));
	
	// Initialize Stop Channel
	//itoa((int)CADCardControl::Instance()->GetCardStopChl, szBuffer, 10);
	
	//SendDlgItemMessage(IDC_ESTOP, EM_REPLACESEL, 0, (LPARAM)((LPSTR)szBuffer));

	// Initialize Triggering
	CheckRadioButton(IDC_INTTRIG, IDC_EXTTRIG,
		(CADCardControl::Instance()->GetCardExtTrig(m_pCard))?IDC_EXTTRIG:IDC_INTTRIG);
	
	// Initialize Repeated Mode
	CheckRadioButton(IDC_CYCLIC, IDC_NONCYCLIC,
		(CADCardControl::Instance()->GetCardCyclicMode(m_pCard))?IDC_CYCLIC:IDC_NONCYCLIC);
	
	// Initialize Buffer Mode
	GetDlgItem(IDC_BUFSINGLE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUFDOUBLE)->EnableWindow(FALSE);
	
	// CheckRadioButton(hDlg, IDC_BUFSINGLE, IDC_BUFDOUBLE,
        //     (gwBufferMode)?IDC_BUFDOUBLE:IDC_BUFSINGLE);

	// Initialize Data Type
	CheckRadioButton(IDC_DATAUSHORT, IDC_DATAFLOAT,
		(CADCardControl::Instance()->GetCardDataType(m_pCard))?IDC_DATAFLOAT:IDC_DATAUSHORT);
	
	// Initialize Message Flag
	CheckRadioButton(IDC_EVTENABLE, IDC_EVTDISABLE,
		(CADCardControl::Instance()->GetCardEvtFlag(m_pCard))?IDC_EVTENABLE:IDC_EVTDISABLE);
	
	// Initialize number of interrupt count
	GetDlgItem(IDC_FIFOSIZE)->EnableWindow(FALSE);
    GetDlgItem(IDC_FIFOTEXT)->EnableWindow(FALSE);

	if (CADCardControl::Instance()->GetCardFifoEnable(m_pCard))
	{
		SendDlgItemMessage(IDC_FifoEnable, BM_SETCHECK, 1, 0);
		GetDlgItem(IDC_FIFOSIZE)->EnableWindow(TRUE);
		GetDlgItem(IDC_FIFOTEXT)->EnableWindow(TRUE);
		itoa((int)CADCardControl::Instance()->GetCardFifoSize(m_pCard), szBuffer, 10);
		SendDlgItemMessage(IDC_FIFOSIZE, EM_REPLACESEL, 0, (LPARAM)((LPSTR)szBuffer));
	}
	
	//SendDlgItemMessage(IDC_CONVINT, EM_REPLACESEL, 0, (LPARAM)((LPSTR)szBuffer));
	// Initialize gain table option
	if (CADCardControl::Instance()->GetCardGainList(m_pCard))
	{
		CheckRadioButton(IDC_OVERALLGAIN, IDC_GAINARRAY, IDC_GAINARRAY);
		GetDlgItem(IDC_INPRANGE)->EnableWindow(FALSE);
		GetDlgItem(IDC_GAINLIST)->EnableWindow(TRUE);
	}
	else
	{
		CheckRadioButton(IDC_OVERALLGAIN, IDC_GAINARRAY, IDC_OVERALLGAIN);
		GetDlgItem(IDC_INPRANGE)->EnableWindow(TRUE);
		GetDlgItem(IDC_GAINLIST)->EnableWindow(FALSE);
    }

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CCardConfigDlg::OnKillfocusEstart() 
{
	// TODO: Add your control notification handler code here
	char szBuffer[100];
	if(SendDlgItemMessage(IDC_ESTART, EM_GETMODIFY, 0, 0))
	{
		SendDlgItemMessage(IDC_ESTART, WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
		CADCardControl::Instance()->SetCardStartChl(m_pCard,(USHORT)atoi(szBuffer));
		SendDlgItemMessage(IDC_ESTART, EM_SETMODIFY, FALSE, 0) ;
    }
}

void CCardConfigDlg::OnFifoEnable() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardFifoEnable(m_pCard, (int)SendDlgItemMessage(IDC_FifoEnable, BM_GETCHECK, 0, 0));
	if(CADCardControl::Instance()->GetCardFifoEnable(m_pCard) == 1)
	{
		GetDlgItem(IDC_FIFOSIZE)->EnableWindow(TRUE);
		GetDlgItem(IDC_FIFOTEXT)->EnableWindow(TRUE);

		int fifo_size = CADCardControl::Instance()->GetCardFifoSizeFeature(m_pCard);
		
		// divide by 2 for conversion from byte to word
		char szFifoSize[10] = {0};
		CADCardControl::Instance()->SetCardFifoSize(m_pCard, (USHORT)fifo_size / 2);
		GetDlgItem(IDC_FIFOSIZE)->SetWindowText(itoa((int)((USHORT)fifo_size / 2), szFifoSize, 10));
	}
	else
	{
		GetDlgItem(IDC_FIFOSIZE)->EnableWindow(FALSE);
		GetDlgItem(IDC_FIFOTEXT)->EnableWindow(FALSE);
	}
}

void CCardConfigDlg::OnOverallgain() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardGainList(m_pCard, 0);
	GetDlgItem(IDC_INPRANGE)->EnableWindow(TRUE);
	GetDlgItem(IDC_GAINLIST)->EnableWindow(FALSE);
}

void CCardConfigDlg::OnGainarray() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardGainList(m_pCard, 1);
	GetDlgItem(IDC_INPRANGE)->EnableWindow(FALSE);
	GetDlgItem(IDC_GAINLIST)->EnableWindow(TRUE);
}

void CCardConfigDlg::OnKillfocusInprange() 
{
	// TODO: Add your control notification handler code here
	int i = 0;
	i = (int)SendDlgItemMessage(IDC_INPRANGE, CB_GETCURSEL, 0, 0) ;
	if (i != CB_ERR)
	{
		CADCardControl::Instance()->SetCardGainCode(m_pCard, (USHORT)i);
	}                   
}

void CCardConfigDlg::OnKillfocusPacer() 
{
	// TODO: Add your control notification handler code here
	char szBuffer[100] = {0};
	if(SendDlgItemMessage(IDC_PACER, EM_GETMODIFY, 0, 0))
	{
		SendDlgItemMessage(IDC_PACER, WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
		CADCardControl::Instance()->SetCardPacerRate(m_pCard, (ULONG)atol(szBuffer));
		SendDlgItemMessage(IDC_PACER, EM_SETMODIFY, FALSE, 0) ;
	}
	
	DWORD racer_rate = CADCardControl::Instance()->GetCardPacerRate(m_pCard);
	CADCardControl::Instance()->SetCardProperty(m_pCard, CFG_AiPacerRate, &racer_rate, sizeof(racer_rate));

	ULONG pLength = sizeof(ULONG);
	LONG pacerBuffer = 0;
	if (SUCCESS == CADCardControl::Instance()->GetCardProperty(m_pCard, CFG_AiPacerRate, &pacerBuffer, &pLength))
	{
		CADCardControl::Instance()->SetCardPacerRate(m_pCard, pacerBuffer);
		sprintf(szBuffer, "%u", CADCardControl::Instance()->GetCardPacerRate(m_pCard));
		SendDlgItemMessage(IDC_PACER, WM_SETTEXT, 10, (LPARAM)(LPSTR)szBuffer);
	}
}

void CCardConfigDlg::OnKillfocusConversion() 
{
	// TODO: Add your control notification handler code here
	char szBuffer[100] = {0};
	if(SendDlgItemMessage(IDC_CONVERSION, EM_GETMODIFY, 0, 0))
	{
		SendDlgItemMessage(IDC_CONVERSION, WM_GETTEXT, 10, (LPARAM)(LPSTR)szBuffer) ;
		CADCardControl::Instance()->SetCardConvNum(m_pCard,(ULONG)atol(szBuffer));
		SendDlgItemMessage(IDC_CONVERSION, EM_SETMODIFY, FALSE, 0);
    }
}

void CCardConfigDlg::OnCyclic() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardCyclicMode(m_pCard, 1);
}

void CCardConfigDlg::OnNoncyclic() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardCyclicMode(m_pCard, 0);
}

void CCardConfigDlg::OnInttrig() 
{
	// TODO: Add your control notification handler code here
    GetDlgItem(IDC_PACER)->EnableWindow(TRUE);
    CADCardControl::Instance()->SetCardExtTrig(m_pCard, 0);
}

void CCardConfigDlg::OnExttrig() 
{
	// TODO: Add your control notification handler code here
    GetDlgItem(IDC_PACER)->EnableWindow(FALSE);
    CADCardControl::Instance()->SetCardExtTrig(m_pCard, 1);
}

void CCardConfigDlg::OnBufsingle() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardBufferMode(m_pCard, 0);
}

void CCardConfigDlg::OnBufdouble() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardBufferMode(m_pCard, 1);
}

void CCardConfigDlg::OnDataushort() 
{
	// TODO: Add your control notification handler code here
    CADCardControl::Instance()->SetCardDataType(m_pCard, 0);
}

void CCardConfigDlg::OnDatafloat() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardDataType(m_pCard, 1);
}

void CCardConfigDlg::OnEvtenable() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardEvtFlag(m_pCard, 1);
}

void CCardConfigDlg::OnEvtdisable() 
{
	// TODO: Add your control notification handler code here
	CADCardControl::Instance()->SetCardEvtFlag(m_pCard, 0);
}
